#include "Coin.h"
#include "Resource.h"

Coin::Coin(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int id_obj_type, int Obj_state) : Object(x, y)//, id_obj_type, obj_state)
{
	this->setIDObjType(id_obj_type);
	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\item_32.png"), 125, 0);

	_yOld = y;;
	veloc_y = 0;
	//_accelY = 0;
	accel_y = 0;

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	switch (this->getIDObjType())
	{
	case 41:	//coin hien
		p_sprite->setFrame(6,6); //frame dong tien hien
		_numberCoin = 1;
		isHidden = false;
		break;
	case 46:	//coin an
		p_sprite->setFrame(7,7); //frame an
		_numberCoin = 1;
		isHidden = true;
		break;
	case 47:	//5 coin an
		p_sprite->setFrame(7,7); //frame an
		_numberCoin = 5;
		isHidden = true;
		break;
	}

	this->obj_state = Obj_state;//IS_IDLE;
}

Coin::~Coin()
{
	if (p_sprite != NULL)
	{
		delete p_sprite;
	}
}

void Coin::render(int cameraX, int cameraY)
{
	if (obj_state != IS_REMOVE)
	{
		p_sprite->render(pos_x, pos_y, cameraX, cameraY);
	}
	
}

void Coin::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	switch (obj_state)
	{
	case IS_IDLE:
		if (_numberCoin == 0)
		{
			changeState(IS_REMOVE);
		}
		break;
	case IS_ACTIVING:
		if (_numberCoin != 0)
		{
			veloc_y -= accel_y * gameTime;
			pos_y += veloc_y * gameTime + 1.0f/2.0f * accel_y * gameTime * gameTime;
			//if ((pos_y > _yOld + 64) || (veloc_y < 0))
			if ((veloc_y < 0))
			{
				_numberCoin--;				
				if (_numberCoin == 0)
				{
					if (this->getIDObjType() == 47)		//coin 5 hidden
					{
						this->container->changeState(BS_NONE);
					}
					changeState(IS_REMOVE);
				}
				else
					changeState(IS_IDLE);
				
			}
		}
		break;
	}
	p_sprite->update(gameTime);
}

void Coin::changeState(int obj_state)
{
	this->obj_state = obj_state;
	switch (this->obj_state)
	{
	case IS_IDLE:
		pos_y = _yOld;
		veloc_y = 0;
		accel_y = 0;
		p_sprite->setFrame(7,7);
		break;
	case IS_ACTIVING:
		veloc_y = 1.5;	
		//_accelY = GRAVITY;
		accel_y = GRAVITY;
		p_sprite->setFrame(6,6);
		break;
	case  IS_REMOVE:
		p_sprite->setFrame(7,7);
		break;
	}
}

OBJECT_TYPE Coin::getObjectType()
{
	return OBJECT_ITEM;
}